<?php
session_start();
require_once('classDB.php');
$d = new db;
 
//session_start();
if ( !isset($_SESSION['daySoLuong']) ) $_SESSION['daySoLuong']=array();
if ( !isset($_SESSION['dayDonGia']) )  $_SESSION['dayDonGia']=array();
if ( !isset($_SESSION['dayTenSP']) )   $_SESSION['dayTenSP']=array();

$idTin = $_GET["idTin"]; 
$action = $_GET['action'];
$soluong =(isset($_GET['soluong'])==true)? $_GET['soluong']:1;

settype($idTin,"int");

settype($soluong,"int");	
if ($soluong<=0) $soluong=1;
if ($soluong>100) $soluong=100;

$act=array("update", "add","remove","removeAll");
if (in_array($action , $act)==false) //exit();
	die("chua chọn action");	


if ($action=="removeAll"){
unset($_SESSION['dayTenSP']);
unset($_SESSION['dayDonGia']);
unset($_SESSION['daySoLuong']);	
exit();	
}

if ($idTin<=0) die(" Không biết sản phẩm nào phải thêm vào giỏ hàng");

if ($action=="remove") {
unset($_SESSION['dayTenSP'][$idTin]);
unset($_SESSION['dayDonGia'][$idTin]);
unset($_SESSION['daySoLuong'][$idTin]);			
}

if ($action=="add") {
	$sql = "SELECT TieuDe,Gia FROM tin WHERE idTin=$idTin";
	$sanpham = mysql_query($sql) or die (mysql_error());	
	$row_sanpham = mysql_fetch_assoc($sanpham);		
	
	$_SESSION['dayTenSP'][$idTin] = $row_sanpham['TieuDe'];
	$_SESSION['dayDonGia'][$idTin] = $row_sanpham['Gia'];
	$_SESSION['daySoLuong'][$idTin] += $soluong;
}

if ($action=="update") {
	$sql = "SELECT TieuDe,Gia FROM tin WHERE idTin=$idTin";
	$sanpham=mysql_query($sql) or die (mysql_error());
	$row_sanpham=mysql_fetch_assoc($sanpham);

	$_SESSION['dayTenSP'][$idTin] = $row_sanpham['TieuDe'];
	$_SESSION['dayDonGia'][$idTin] = $row_sanpham['Gia'];
	$_SESSION['daySoLuong'][$idTin] = $soluong;			
}


include('hiengiohang_update.php');
?>